﻿Imports System.Environment
Imports System.Globalization

Partial Public Class Test_SAP
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then

        End If
    End Sub

    Private Sub btnInvoke_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnInvoke.Click
        Dim Connector As New SAP.Connector.Destination
        Dim SAP_Data As Object
        SAP_Data = New SAP_IPORT.SAP_IPORT_FUNCTION

        Connector.AppServerHost = "10.16.19.40"
        Connector.SystemNumber = 10
        Connector.Client = 900
        ' Connector.Username = "SDRFC05"
        ' Connector.Password = "uio37dfg"

        Connector.Username = "dm_ws1"
        Connector.Password = "sapiport0"

        SAP_Data.Connection = New SAP.Connector.SAPConnection(Connector)
        Try
            SAP_Data.Connection.Open()

            Try
                Select Case DropDownList1.SelectedValue
                    'Zbapi_Iport_Advanced_Payment
                    Case "1"
                        Dim ZBABI_BK As New SAP_IPORT.ZBAPI_BANK
                        'ZBABI_BK.Amount = "1327.04"
                        ZBABI_BK.Amount = "1316.10"
                        ZBABI_BK.Gl_Account = "0010302031"
                        ZBABI_BK.Item_Text = "TR KBANK"
                        ' ZBABI_BK.Value_Date = Date.Now.ToString("yyyyMMdd", New CultureInfo("en-US"))
                        ZBABI_BK.Value_Date = "20101230"

                        Dim ZBABI_HD As New SAP_IPORT.ZBAPI_HEADER
                        ZBABI_HD.Bupla = "0000"
                        ' ZBABI_HD.Doc_Date = Date.Now.ToString("yyyyMMdd", New CultureInfo("en-US"))
                        ZBABI_HD.Doc_Date = "20101230"
                        ' ZBABI_HD.Header_Txt = "R|LEO GLORY"
                        ZBABI_HD.Header_Txt = "R|SUN INVESTOR"
                        'ZBABI_HD.Pstng_Date = Date.Now.ToString("yyyyMMdd", New CultureInfo("en-US"))
                        ZBABI_HD.Pstng_Date = "20101230"

                        Dim ZBABI_VT As New SAP_IPORT.ZBAPI_VAT
                        'ZBABI_VT.Amount = "1240.22"
                        'ZBABI_VT.Vat = "86.82"
                        ZBABI_VT.Amount = "1230.00"
                        ZBABI_VT.Vat = "86.10"

                        Dim Ex_Bukrs As String = ""
                        Dim Ex_Gjahr As String = ""
                        Dim Ex_Belnr As String = ""
                        Dim Customer As New SAP_IPORT.ZBAPI_CUSTOMER
                        ' Customer.Customer = "201390"
                        Customer.Customer = "100108"
                        Customer.Amount = "1230.00"
                        Customer.Item_Text = "PORT DUES"
                        Customer.Material = "2100000200"
                        Customer.Price = "1000"
                        Customer.Quantity = "1.23"
                        Customer.Unit = "TON"
                        Customer.Wht_Code = "05"

                        'Dim Customer2 As New SAP_IPORT.ZBAPI_CUSTOMER
                        'Customer2.Customer = "100108"
                        ''Customer2.Customer = "201390"
                        'Customer2.Amount = "10.22"
                        'Customer2.Item_Text = "CARGO WHARFAGE"
                        'Customer2.Material = "2100000281"
                        'Customer2.Price = "0.50"
                        'Customer2.Quantity = "20.43"
                        'Customer2.Unit = "TON"
                        'Customer2.Wht_Code = "05"

                        Dim CustomerTable As New SAP_IPORT.ZBAPI_CUSTOMERTable
                        CustomerTable.Insert(0, Customer)
                        '  CustomerTable.Insert(1, Customer2)
                        'ID_IPORT =ปีเดือนวัน-ลำดับ
                        Dim BABITable As New SAP_IPORT.BAPIRET1Table
                        SAP_Data.Zbapi_Iport_Advanced_Payment(ZBABI_BK, ZBABI_HD, "20110199-999", "", ZBABI_VT, Ex_Belnr, Ex_Bukrs, Ex_Gjahr, CustomerTable, BABITable)
                        Dim Message As String = "Advance Payment" & Environment.NewLine
                        For i As Integer = 0 To BABITable.Count - 1
                            Message &= BABITable.Item(i).Message & Environment.NewLine
                        Next


                        'CASH SALE CREATE SALE ORDER
                        Dim BABIHEADER As New SAP_IPORT.ZBAPI_SALES_HEADER
                        BABIHEADER.Doc_Type = "ZSRC"
                        ' BABIHEADER.Req_Date_H = Date.Now.ToString("yyyyMMdd", New CultureInfo("en-US"))
                        BABIHEADER.Req_Date_H = "20101230"
                        BABIHEADER.Sold_To = "0000100108"
                        BABIHEADER.Bill_To = "0000100108"
                        BABIHEADER.Ship_Name = "LEO GLORY"

                        Dim BABIONETIME As New SAP_IPORT.ZBAPI_SALES_ONETIME

                        Dim EX_SALES_NO As String = ""
                        Dim EX_BILL_NO As String = ""
                        Dim EX_BUKRS2 As String = ""
                        Dim EX_BELNR2 As String = ""
                        Dim EX_GJAHR2 As String = ""

                        Dim BABI_SALE As New SAP_IPORT.ZBAPI_SALES_ITEMS
                        BABI_SALE.Material = "2100000200"
                        BABI_SALE.Plant = "1103"
                        BABI_SALE.Target_Qty = "1.23"
                        BABI_SALE.Target_Qu = "TON"
                        BABI_SALE.Cond_Type = ""
                        BABI_SALE.Discount = "0"
                        BABI_SALE.Amount = "1000"

                        Dim SALE_TABLE As New SAP_IPORT.ZBAPI_SALES_ITEMSTable
                        SALE_TABLE.Insert(0, BABI_SALE)

                        'BABI_SALE = New SAP_IPORT.ZBAPI_SALES_ITEMS
                        'BABI_SALE.Material = "000000002100000281"
                        'BABI_SALE.Plant = "1103"
                        'BABI_SALE.Target_Qty = "20.43"
                        'BABI_SALE.Target_Qu = "TON"
                        'BABI_SALE.Cond_Type = ""
                        'BABI_SALE.Discount = "0"
                        'BABI_SALE.Amount = "0.50"

                        'SALE_TABLE.Insert(0, BABI_SALE)
                        BABITable = New SAP_IPORT.BAPIRET1Table
                        SAP_Data.Zbapi_Iport_Sd_Proc_Cash_Sales("20101230", "", "20110199-999", BABIONETIME, BABIHEADER, EX_BELNR2, EX_BILL_NO, EX_BUKRS2, EX_GJAHR2, EX_SALES_NO, BABITable, SALE_TABLE)

                        Message &= "CAST SALE" & Environment.NewLine
                        For i As Integer = 0 To BABITable.Count - 1
                            Message &= BABITable.Item(i).Type & " - " & BABITable.Item(i).Id & " - " & BABITable.Item(i).Number & " - " & BABITable.Item(i).Message & Environment.NewLine
                        Next
                        ' E - VF - 140 - Document cannot be billed due to credit limit check
                        TextBox2.Text = Message

                        'Zbapi_Iport_Ar_Clear_Item
                    Case "2"
                        Dim CompanyCode As String = "1001"
                        Dim CustomerCode As String = "0000100108"
                        Dim DateFrom As String = "20100701"
                        Dim DateTo As String = "20100730"
                        Dim EX_SUBRC As String = ""
                        Dim BABI_ARCLEAR As New SAP_IPORT.ZBAPI_AR_CLEARTable
                        SAP_Data.Zbapi_Iport_Ar_Clear_Item(CompanyCode, CustomerCode, DateFrom, DateTo, EX_SUBRC, BABI_ARCLEAR)
                        If (BABI_ARCLEAR.Count > 0) Then
                            For i As Integer = 0 To BABI_ARCLEAR.Count - 1
                                Dim ClearDate As String = BABI_ARCLEAR.Item(i).Clear_Date
                            Next
                        End If
                        TextBox2.Text = BABI_ARCLEAR.Count.ToString()
                        'Zbapi_Iport_Ar_Open_Item
                    Case "3"
                        Dim CompanyCode As String = "1001"
                        Dim CustomerCode As String = "0000100108"
                        Dim EX_SUBRC As String = "0"  'ถ้ามีค่าเป็น 0 คือพบข้อมูล
                        Dim BABI_ARTABLE As New SAP_IPORT.ZBAPI_AR_OPENTable
                        SAP_Data.Zbapi_Iport_Ar_Open_Item(CompanyCode, CustomerCode, EX_SUBRC, BABI_ARTABLE)
                        If (BABI_ARTABLE.Count > 0) Then
                            For i As Integer = 0 To BABI_ARTABLE.Count - 1
                                Dim XBLNR As String = BABI_ARTABLE.Item(i).Xblnr  'คือ Billing ใช้หาเรือต่อได้
                                Dim Bill_TO As String = BABI_ARTABLE.Item(i).Buzei
                                Dim Taxclass As String = BABI_ARTABLE.Item(i).Mwskz  'ตัวภาษี
                            Next
                        End If
                        TextBox2.Text = BABI_ARTABLE.Count.ToString() & BABI_ARTABLE.Item(3).Netamt.ToString()

                        'Zbapi_Iport_Income_Payment
                    Case "4"
                        Dim CompanyCode As String = "1001"
                        Dim CustomerCode As String = "0000100108"
                        Dim EX_SUBRC As String = "0"  'ถ้ามีค่าเป็น 0 คือพบข้อมูล
                        Dim BABI_ARTABLE As New SAP_IPORT.ZBAPI_AR_OPENTable
                        SAP_Data.Zbapi_Iport_Ar_Open_Item(CompanyCode, CustomerCode, EX_SUBRC, BABI_ARTABLE)
                        'If (BABI_ARTABLE.Count > 0) Then
                        '    For i As Integer = 0 To BABI_ARTABLE.Count - 1
                        '        Dim XBLNR As String = BABI_ARTABLE.Item(i).Xblnr  'คือ Billing ใช้หาเรือต่อได้
                        '        Dim Bill_TO As String = BABI_ARTABLE.Item(i).Buzei
                        '    Next
                        'End If
                        Dim BABI_CLEARING As SAP_IPORT.ZBAPI_CLEARING
                        Dim BABI_CLEARINGTB As New SAP_IPORT.ZBAPI_CLEARINGTable
                        Dim sumAmount As Double = 0
                        For i As Integer = 0 To BABI_ARTABLE.Count - 1
                            BABI_CLEARING = New SAP_IPORT.ZBAPI_CLEARING
                            BABI_CLEARING.Comp_Code = BABI_ARTABLE.Item(i).Bukrs
                            BABI_CLEARING.Fisc_Year = BABI_ARTABLE.Item(i).Gjahr
                            BABI_CLEARING.Ac_Doc_No = BABI_ARTABLE.Item(i).Belnr
                            BABI_CLEARING.Blart = BABI_ARTABLE.Item(i).Blart
                            BABI_CLEARING.Waers = BABI_ARTABLE.Item(i).Waers
                            BABI_CLEARING.Whtamt = BABI_ARTABLE.Item(i).Whtamt
                            BABI_CLEARINGTB.Insert(0, BABI_CLEARING)
                            sumAmount += BABI_ARTABLE.Item(i).Netamt
                        Next
                        Dim EX_BUKRS As String = ""
                        Dim EX_BELNR As String = ""
                        Dim EX_GJAHR As String = ""
                        Dim IPORT_NUMBER As String = "20100802-006"
                        Dim ZBABI_BK As New SAP_IPORT.ZBAPI_BANK
                        ZBABI_BK.Amount = sumAmount.ToString("##########.##")
                        ZBABI_BK.Gl_Account = "0010302031"
                        ZBABI_BK.Item_Text = "TR KBANK"
                        ZBABI_BK.Value_Date = "20100802"

                        Dim ZBABI_HD As New SAP_IPORT.ZBAPI_HEADER
                        ZBABI_HD.Bupla = "0000"
                        ZBABI_HD.Doc_Date = "20100802"
                        ZBABI_HD.Header_Txt = "R|LEO GLORY"
                        ZBABI_HD.Pstng_Date = "20100802"
                        Dim BABITable As New SAP_IPORT.BAPIRET1Table

                        SAP_Data.Zbapi_Iport_Income_Payment(ZBABI_BK, CustomerCode, ZBABI_HD, IPORT_NUMBER, EX_BELNR, EX_BUKRS, EX_GJAHR, BABI_CLEARINGTB, BABITable)
                        Dim Message As String = "INCOME" & Environment.NewLine
                        For i As Integer = 0 To BABITable.Count - 1
                            Message &= BABITable.Item(i).Message & Environment.NewLine
                        Next

                        TextBox2.Text = Message
                        'Zbapi_Iport_Sd_Proc_Cr_Sales
                    Case "5"
                        Dim BABIHEADER As New SAP_IPORT.ZBAPI_SALES_HEADER
                        BABIHEADER.Doc_Type = "ZSRR"
                        BABIHEADER.Req_Date_H = "20100921"
                        BABIHEADER.Sold_To = Txtid.Text  ' ONE TIME ใส่ OND001
                        BABIHEADER.Bill_To = Txtid.Text
                        BABIHEADER.Ship_Name = "LEO GLORY"

                        Dim BABIONETIME As New SAP_IPORT.ZBAPI_SALES_ONETIME
                        'BABIONETIME.Name = "AAA"
                        'BABIONETIME.Name_2 = "BBB"
                        'BABIONETIME.Name_3 = "CCC"
                        'BABIONETIME.City = "CITY"
                        'BABIONETIME.District = "DISTRICT"
                        'BABIONETIME.Location = "LOCATION"
                        'BABIONETIME.Postl_Cod1 = "POSTAL"
                        'BABIONETIME.Str_Suppl3 = "SUP"
                        'BABIONETIME.Street = "STREET"

                        Dim EX_SALES_NO As String = ""
                        Dim EX_BILL_NO As String = ""

                        Dim BABI_SALE As New SAP_IPORT.ZBAPI_SALES_ITEMS
                        BABI_SALE.Material = "000000002100000200"
                        BABI_SALE.Plant = "1103"
                        BABI_SALE.Target_Qty = "10.44"
                        BABI_SALE.Target_Qu = "TON"
                        BABI_SALE.Cond_Type = ""
                        BABI_SALE.Discount = "0"
                        BABI_SALE.Amount = "1000"

                        Dim SALE_TABLE As New SAP_IPORT.ZBAPI_SALES_ITEMSTable
                        SALE_TABLE.Insert(0, BABI_SALE)

                        BABI_SALE = New SAP_IPORT.ZBAPI_SALES_ITEMS
                        BABI_SALE.Material = "000000002100000281"
                        BABI_SALE.Plant = "1103"
                        BABI_SALE.Target_Qty = "20.62"
                        BABI_SALE.Target_Qu = "TON"
                        BABI_SALE.Cond_Type = ""
                        BABI_SALE.Discount = "0"
                        BABI_SALE.Amount = "0.50"

                        SALE_TABLE.Insert(0, BABI_SALE)

                        BABI_SALE = New SAP_IPORT.ZBAPI_SALES_ITEMS
                        BABI_SALE.Material = "000000002100000256"
                        BABI_SALE.Plant = "1103"
                        BABI_SALE.Target_Qty = "15.43"
                        BABI_SALE.Target_Qu = "D"
                        BABI_SALE.Cond_Type = ""
                        BABI_SALE.Discount = "0"
                        BABI_SALE.Amount = "300.00"

                        SALE_TABLE.Insert(0, BABI_SALE)

                        Dim BABITable As New SAP_IPORT.BAPIRET1Table
                        Dim Bill_Date As String = Date.Now.ToString("yyyyMMdd", New CultureInfo("en-US"))
                        SAP_Data.Zbapi_Iport_Sd_Proc_Cr_Sales(Bill_Date, "", "20100921-777", BABIONETIME, BABIHEADER, EX_BILL_NO, EX_SALES_NO, BABITable, SALE_TABLE)
                        Dim Message As String = ""
                        Message &= "Credit SALE" & Environment.NewLine
                        For i As Integer = 0 To BABITable.Count - 1
                            Message &= BABITable.Item(i).Message & Environment.NewLine
                        Next

                        TextBox2.Text = Message
                        'ZBAPI_IPORT_SD_CN_DN_REQUEST
                    Case "6"
                        Dim Doc_TYPE As String = "ZCR"     'CN = ZCR ใหม่น้อยกว่าเก่า , DN = ZDR  ใหม่มากกว่าเก่า
                        Dim IPROT_NUMBER As String = "20100921-006"
                        Dim Reason As String = "Z01"   'เหตุผล  ZO1   DN, Z02  CN, Z03 Quatity Change(Target_Qty ใส่ตามจำนวนเต็ม)
                        Dim REF_DOC As String = "9110091008"  'Billing
                        'Z01 ราคาลด  ,Z02 ราคาเพิ่ม  , Z03 เปลี่ยน Quatity
                        Dim EX_Sale As String = ""  'Sale Number
                        Dim Due_Date As String = ""
                        Dim CNDN_TABLE As New SAP_IPORT.ZBAPI_CN_DN_ITEMSTable
                        Dim CNDN As New SAP_IPORT.ZBAPI_CN_DN_ITEMS
                        'CNDN.Material = "000000002100000256"
                        'CNDN.Plant = "1103"
                        'CNDN.Target_Qty = "1.43"  ' CN
                        'CNDN.Target_Qu = "D"
                        'CNDN.Cond_Type = "ZNET"  'ZNET
                        'CNDN.Amount = "300.00"
                        'CNDN_TABLE.Insert(0, CNDN)
                        CNDN.Material = "000000002100000200"
                        CNDN.Plant = "1103"
                        CNDN.Target_Qty = "1"  'DN
                        CNDN.Target_Qu = "TON"
                        CNDN.Cond_Type = "ZNET"  'ZNET
                        CNDN.Amount = "500"
                        CNDN_TABLE.Insert(0, CNDN)

                        Dim ZBABI_RET As New SAP_IPORT.BAPIRET1Table

                        SAP_Data.Zbapi_Iport_Sd_Cn_Dn_Request(Doc_TYPE, IPROT_NUMBER, Reason, REF_DOC, EX_Sale, CNDN_TABLE, ZBABI_RET)

                        'ZBAPI_IPORT_SD_CN_DN_RELEASE
                        Dim Message As String = ""
                        Message &= "REQUEST" & Environment.NewLine
                        For i As Integer = 0 To ZBABI_RET.Count - 1
                            Message &= ZBABI_RET.Item(i).Message & Environment.NewLine
                        Next
                        ViewState("Ex_Sale") = EX_Sale
                        ViewState("IPROT_NUMBER") = IPROT_NUMBER
                        ViewState("ZBABI_RET") = ZBABI_RET

                        TextBox2.Text = Message

                    Case "7"
                        Dim Message As String = ""
                        Dim iportNumber As String = "20100921-006"
                        Dim ZBABI_RET As New SAP_IPORT.BAPIRET1Table
                        Dim Bill_no As String = ""
                        SAP_Data.Zbapi_Iport_Sd_Cn_Dn_Release(iportNumber, Bill_no, ZBABI_RET)
                        Message &= "RELESE" & Environment.NewLine
                        For i As Integer = 0 To ZBABI_RET.Count - 1
                            Message &= ZBABI_RET.Item(i).Message & Environment.NewLine
                        Next
                        TextBox2.Text = Message
                    Case "8"
                        Dim TB_MESSAGE As New SAP_IPORT.BAPIRET1Table
                        Dim SO_INPUT As New SAP_IPORT.ZBAPI_SO_INPUTTable
                        Dim SO_IN As New SAP_IPORT.ZBAPI_SO_INPUT
                        SO_IN.Doc_Number = "1280000490"
                        SO_INPUT.Insert(0, SO_IN)
                        Dim EX_BUKRS As String = ""
                        Dim EX_BELNR As String = ""
                        Dim EX_GJAHR As String = ""
                        Dim Message As String = ""
                        SAP_Data.Zbapi_Iport_Release_Cr_Limited(EX_BELNR, EX_BUKRS, EX_GJAHR, TB_MESSAGE, SO_INPUT)
                        Message &= "RELESE_CR" & Environment.NewLine
                        For i As Integer = 0 To TB_MESSAGE.Count - 1
                            Message &= TB_MESSAGE.Item(i).Message & Environment.NewLine
                        Next

                        TextBox2.Text = Message
                    Case "9" 'Zbapi_Iport_Sales_Order_Change
                        Dim IM_SALES_ORDER As String = "1290002023"
                        Dim ZBABI_RET As New SAP_IPORT.BAPIRET1Table
                        Dim ZBAPI_SALES_ITEMS_CHG As New SAP_IPORT.ZBAPI_SALES_ITEMS_CHG
                        Dim ZBAPI_SALES_ITEMS_CHGTable As New SAP_IPORT.ZBAPI_SALES_ITEMS_CHGTable
                        ZBAPI_SALES_ITEMS_CHG.Updateflag = "U"
                        ZBAPI_SALES_ITEMS_CHG.Material = "000000002100000200"
                        ZBAPI_SALES_ITEMS_CHG.Target_Qty = "20"
                        ZBAPI_SALES_ITEMS_CHG.Target_Qu = "TON"
                        ZBAPI_SALES_ITEMS_CHG.Plant = "1103"
                        ZBAPI_SALES_ITEMS_CHG.Cond_Type = "ZB05"
                        ZBAPI_SALES_ITEMS_CHG.Cond_Amount = 800
                        ZBAPI_SALES_ITEMS_CHG.Disc_Type = ""
                        ZBAPI_SALES_ITEMS_CHG.Disc_Amount = 0
                        ZBAPI_SALES_ITEMS_CHGTable.Insert(0, ZBAPI_SALES_ITEMS_CHG)
                        SAP_Data.Zbapi_Iport_Sales_Order_Change(IM_SALES_ORDER, ZBABI_RET, ZBAPI_SALES_ITEMS_CHGTable)
                        Dim Message As String = ""
                        Message &= "Change ORder" & Environment.NewLine
                        For i As Integer = 0 To ZBABI_RET.Count - 1
                            Message &= ZBABI_RET.Item(i).Message & Environment.NewLine
                        Next

                        TextBox2.Text = Message
                End Select
            Catch ex As Exception
                TextBox2.Text = ex.Message
            End Try
        Catch ex As Exception
            TextBox2.Text = ex.Message
        End Try
        SAP_Data.Connection.Close()
    End Sub
End Class
